﻿/*
**	Scrolling background with effects
*/

#pragma once

#include "global_state.h"
#include "resources.h"
#include "player.h"

struct Effect_Particle{
	CL_Vec2<float> position;
	CL_Vec2<float> move;
	CL_Angle angle;
	CL_Color color;
	unsigned int lifespan;
	unsigned int lived;

	float speed;
	float rotation_speed;
	float size;
	int anim;
	int p_type;

	float current_zoom;
	float fade;
};

struct BG_Effect{
	CL_String name;
	CL_String sprite;
	CL_Sprite* _sprite;

	int effect_type;
	/*
			0 - зарезервировано
			1 - подкраска фона
			2 - падающие частицы
			3 - облака
			4 - деревья
	*/

	int effect_index;

	float speed;
	float speed_min;
	float speed_max;
	float scroll_speed;
	float rotation_min;
	float rotation_max;
	float life_min;
	float life_max;
	float fade;
	float size_min;
	float size_max;
	float zoom;
	float spawn_field;
	CL_Color color;
	CL_Color rand_color;
	float alpha;

	int follow_layer;

	int fill_left;
	int fill_right;
	int fill_center;
	float last_left;
	float last_right;
	float last_center;

	std::vector<Effect_Particle> effect_particles;

	unsigned int num_particles;
	unsigned int max_particles;
	float spawn_interval;
	float time_from_last_spawn;
	float time_to_next_spawn;
};

struct Layer{
	int map_width;
	int map_height;

	int tile_width;
	int tile_height;

	int layer_width;
	int layer_height;

	float speed_x;
	float speed_y;

	float offset_x;
	float offset_y;

	float current_pos_x;
	float current_pos_y;

	float zoom_x;
	float zoom_y;

	float rand_x; // Пределы добавления рандомной скорости
	float rand_y; //
	float rand_speed; // Изменение рандомной скорости
	float rand_next_x; // Следующее значение
	float rand_next_y;
	float rand_current_x; // Текущее значение
	float rand_current_y;

	float alpha;

	// Эффекты в действии
	int action;
	float action_time;
	float action_end_time;
	float action_result;
	float action_step;

	std::vector<int> map;

	CL_String name;
	CL_String sprite;
	CL_Sprite* _sprite;
};

class Background
{
public:
	Background();
	~Background();
	
	void load(CL_GraphicContext &gc, const CL_String &level, CL_ResourceManager &resources, CL_Rect v_rect);
	void draw(CL_GraphicContext &gc);
	void set_speed(float delta_speed);
	void scroll(float time);

	void bomb_blow();
	void player_blow();

	void change_layer_alpha(CL_String layer_name, float target_alpha, float timing);
	void show_layer(CL_String layer_name, float timing);
	void hide_layer(CL_String layer_name, float timing);

	void unload();

	std::vector<Layer> layers;
	std::vector<BG_Effect> effects;

	CL_Sprite* sprite_store[200];
	unsigned int sprite_num;

	CL_Colorf background_color;

	bool looped;
	float speed;
	CL_Rect visible_rect;
	int origin_x;
	int origin_y;
	int clear_flag;

	// Перспектива
	bool perspective;
	float lens_zoom;
	float lens_near;
	float lens_far;
	float lens_aspect;
	float fov;
	float aspect;
	CL_Mat4f projection_matrix;
	CL_Mat4f modelview_matrix;
	CL_Angle perspective_angle;
};

extern Background background;
extern Background boss_background;
